package com.zhx.tool;

import com.sun.org.apache.regexp.internal.RESyntaxException;

import java.sql.*;
import java.util.ResourceBundle;

/**
 * @program: web-framework
 * @author: zhx
 * @create: 2021-03-19 16:53
 * 定义一个jdbc连接数据库的工具类
 **/
public class JdbcConnection {
    private static String driver;
    private static String url;
    private static String user;
    private static String password;

    static {
        ResourceBundle dbconn = ResourceBundle.getBundle("dbconn");
        driver = dbconn.getString("db.driver");
        url = dbconn.getString("db.url");
        user = dbconn.getString("db.user");
        password = dbconn.getString("db.password");
        try {
            //1,注册驱动
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //1,获取连接
    public static Connection getconn() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    //2,释放连接
    public static void closeAll(ResultSet resultSet, Statement statement, Connection connection) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        resultSet = null;
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        statement = null;
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        connection = null;
    }
}
